package org.mymail.mail;

import java.io.UnsupportedEncodingException;
import java.util.*;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;

public class HTMLMessage {

	public void initializeMessage(
			HashMap<Address, SubscriberDetails> addrDetailsMap, int mailFwdType) {
		try {
			Set<Address> address = addrDetailsMap.keySet();
			Address address2;
			int fileId;
			javax.mail.Session session = (new SmtpMailSession())
					.createSession();
			for (Iterator<Address> iterator = address.iterator(); iterator
					.hasNext(); FileUtil.updateLastReceived(address2,
					mailFwdType, fileId)) {
				Message message = new MimeMessage(session);
				address2 = (Address) iterator.next();
				SubscriberDetails subDet = (SubscriberDetails) addrDetailsMap
						.get(address2);
				fileId = constructMessage(message, address2, mailFwdType,
						subDet);
				Transport.send(message);
			}

		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	private int constructMessage(Message message, Address address2,
			int mailFwdType, SubscriberDetails subDet)
			throws MessagingException {
		BodyPart messageBodyPart = new MimeBodyPart();
		String htmlText = (new StringBuilder(String.valueOf(FileUtil
				.readFile(MailConstants.welcomeStringFile)))).append(" ")
				.append(subDet.getName()).append("</H3>").append(
						"<img src=\"cid:tabimage\">").append(
						FileUtil.readFile(MailConstants.notificationsFile))
				.append(" ").append(subDet.getSubscribedBy() + "<BR><HR>")
				.append(
						FileUtil.readFile(MailConstants.subscribeFriends)
								+ "<BR><HR>").append(
						FileUtil.readFile(MailConstants.unSubscribe)
								+ "<BR><HR>").append(
						FileUtil.readFile(MailConstants.signatureFile))
				.toString();
		messageBodyPart.setContent(htmlText, "text/html");
		MimeMultipart multipart = new MimeMultipart("related");
		multipart.addBodyPart(messageBodyPart);
		messageBodyPart = new MimeBodyPart();
		int fileId = Recipients.getRecipientsFileDetails(address2, mailFwdType);
		java.io.File file = FileUtil.getFileOnID(mailFwdType, fileId);
		javax.activation.DataSource fds = new FileDataSource(file);
		messageBodyPart.setDataHandler(new DataHandler(fds));
		messageBodyPart.setHeader("Content-ID", "<tabimage>");
		multipart.addBodyPart(messageBodyPart);
		String subject = (new StringBuilder(String.valueOf(MailFwdType
				.getStringForm(mailFwdType)))).append(" No").append(fileId + 1)
				.toString();
		message.setSubject(subject);
		//message.setSubject("TESTING V1.1");
		message.setContent(multipart);
		try {
			message.setFrom(new InternetAddress("sharath.ravindran2@gmail.com",
					"Sharath"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		message.setRecipient(javax.mail.Message.RecipientType.TO, address2);
		System.err.println("TO Message : " + address2.toString());
		return fileId;
	}
}